<%@ page language="java" import="java.util.*" language="java" import="com.jspsmart.upload.*,java.sql.*"  pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<%@ page import="GridCommodity.GridCommodityDeal"%>
<html>
  <head>
   
    <title>将上传的格子铺商品信息保存到数据库</title>

  </head>
  
  <body>
 <%
 	
	   //不超过2M
	   int fileSizeAllow=2097152;
	   //可以用中文 
	   request.setCharacterEncoding("gbk");
	   // 新建一个SmartUpload对象
	    SmartUpload su = new SmartUpload();
	    GridCommodityDeal GridCommodity=new GridCommodityDeal();

	    // 上传初始化
	    su.initialize(pageContext);
	    // 上传文件
	    su.upload();
	    //取得指定位移处的文件对象file
	    com.jspsmart.upload.File file = su.getFiles().getFile(0);
	    
	    if (file.isMissing()){//文件是否存在
   %>
    <script language=javascript>
    alert("请先选择要上传的图片");
    window.location='RoverCommodity.jsp';
    </script>
    
   <%}
        //取得上载的文件的文件名
   		String fileName=file.getFileName();
   		//取得文件的大小
        int fileSize = file.getSize();
        String extName = file.getFileExt().toLowerCase();
        //取得后缀名
        // 将上传文件全部保存到指定目录
        if (fileSize > fileSizeAllow) {			
	   %>
	    <script language=javascript>
	    alert("文件太大了 ");
	    window.location='RoverCommodity.jsp';
	    </script>
	    
	   <%}
        if (!(extName.equals("gif")||extName.equals("jpg") )) {
	   %>
	    <script language=javascript>
	    alert("图片格式不正确  ");
	    window.location='GridCommodity.jsp';
	    </script>
	    
	   <%}
	   String nowTime=new java.util.Date().toLocaleString().replaceAll(":","-").replaceAll(" ","-");
	   String fileNameNew=nowTime+"."+ extName;// 以日期命名的格式
	   file.saveAs("/UpLoadFile/"+fileNameNew, SmartUpload.SAVE_VIRTUAL);
	   String saveur="UpLoadFile"+"/"+fileNameNew;
	    String saveurl=request.getRealPath("/")+saveur;
	    //从跳转前页面获取参数 
		GridCommodity.setComID(GridCommodity.CalGCID());
        GridCommodity.setComName(su.getRequest().getParameter("ComName2"));
		GridCommodity.setConKind((String)su.getRequest().getParameter("ConKind2"));
		GridCommodity.setShowDate(GridCommodity.GCTime);
		GridCommodity.setDescribe((String)su.getRequest().getParameter("Describe2"));
		GridCommodity.setRemCount(Integer.parseInt(su.getRequest().getParameter("ComNum2")));
		GridCommodity.setViewCount(0);
		GridCommodity.setSellCount(0);
		GridCommodity.setComPrice((float)Double.parseDouble(su.getRequest().getParameter("ComPrice2")));
		GridCommodity.setComInf(su.getRequest().getParameter("ComInf2"));
		GridCommodity.setRecency(Integer.parseInt(su.getRequest().getParameter("Recency")));
		String SellerID=(String)session.getAttribute("id");
		out.print("<script>alert('"+SellerID+"')</script>");
		GridCommodity.setGridID(GridCommodity.CalGridID(SellerID));
		GridCommodity.setComImage(saveur);
	    //out.print("<script>alert('"+saveurl+"')</script>");
	    if(GridCommodity.AddGridCommodity(GridCommodity.getComID(),GridCommodity.getComName(),GridCommodity.getConKind(),GridCommodity.getShowDate(),GridCommodity.getDescribe(),GridCommodity.getComImage(),
	    		GridCommodity.getSellCount(),GridCommodity.getRemCount(),GridCommodity.getViewCount(),GridCommodity.getComPrice(),GridCommodity.getComInf(),GridCommodity.getGridID(),GridCommodity.getRecency())==0)
		{
	       out.print("<script>alert('上传商品信息失败 ，请重新尝试  ')</script>");
	    }
	    else
	    {
		   out.print("<script>alert('"+GridCommodity.getShowDate()+"上传商品成功，商品编号为" +GridCommodity.getComID()+"')</script>");
		   
	    }
%>
   <script type="text/javascript">

   window.location.href="../index.jsp";    

  </script>
  </body>
</html>
